package com.fattyCorps.battle.service.battle.data;

import cn.hutool.extra.spring.SpringUtil;
import com.fattyCorps.common.mgr.GlobalConfig;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

public class Team {
    @Getter
    @Setter
    List<BattleHero> heroes = new ArrayList<>();

    GlobalConfig globalConfig;

    public Team() {
        globalConfig = SpringUtil.getBean(GlobalConfig.class);
    }

    public boolean allDead() {
        for (BattleHero hero : heroes) {
            if (!hero.isDead()) {
                return false;
            }
        }

        return true;
    }

    public BattleHero getFirst() {
        for (BattleHero hero : heroes) {
            if (!hero.isDead()) {
                return hero;
            }
        }

        return null;
    }

    // 前排,没有就返回后排
    public List<BattleHero> getFront() {
        return getFront(true);
    }

    public List<BattleHero> getFront(boolean getBack) {
        int maxFrontNum = globalConfig.getIntValue(GlobalConfig.MAX_BATTLE_GROUP_FRONT_HERO_NUM);
        List<BattleHero> retList = new ArrayList<>();

        for (BattleHero hero : heroes) {
            if (hero.getStandPosNo() <= maxFrontNum && !hero.isDead()) {
                retList.add(hero);
            }
        }

        if (retList == null && getBack) {
            return getBack(false);
        }

        return retList;
    }

    // 后排,没有就返回前排
    public List<BattleHero> getBack() {
        return getBack(true);
    }

    public List<BattleHero> getBack(boolean getFront) {
        int maxFrontNum = globalConfig.getIntValue(GlobalConfig.MAX_BATTLE_GROUP_FRONT_HERO_NUM);
        int maxBackNum = globalConfig.getIntValue(GlobalConfig.MAX_BATTLE_GROUP_BACK_HERO_NUM);

        int maxStandNum = maxFrontNum + maxBackNum;

        List<BattleHero> retList = new ArrayList<>();

        for (BattleHero hero : heroes) {
            if (hero.getStandPosNo() > maxFrontNum && hero.getStandPosNo() <= maxStandNum && !hero.isDead()) {
                retList.add(hero);
            }
        }

        if (retList == null && getFront) {
            return getFront(false);
        }

        return retList;
    }
}